如何解决第 1 组和第 2 组的 csv 结果未显示
下面的代码从本地计算机获取一个 CSV 文件并将其显示在主面板上。不幸的是,该代码没有运行,这意味着它在选择“set1”、“set2”等选项时不会显示预期的 CSV 结果。我是 R 的新手.. 谁能帮我解决这个问题?
library(here)
library(shiny)
set1_path <- here("set1.csv")
set2_path <-here("set2.csv")
set3_path <-here("set3.csv")
set4_path <- here("set4.csv")
set1<- read.csv(set1_path)
set2 <- read.csv(set2_path)
set3 <- read.csv(set3_path)
set4 <- read.csv(set4_path)
options(shiny.maxRequestSize=30*1024^2)
shinyApp(
ui = tagList(
navbarPage(
theme = "spacelab",tabPanel("report extracting",sidebarPanel(
fileInput("file1","Select datasets:",accept = c(
"text/csv","text/comma-separated-values,text/plain",".csv"),),# tags$hr(),# checkBoxInput("header","Header",TRUE),# textInput("txt","Study info:","Study name read"),# sliderInput("slider","Tables to read:",1,100,30),# tags$h5("Prepare extraction"),selectInput("pdfExtract1","Pick a Domain",choices = c("domain1","domain2","domain3","domain4")),tableOutput("preview"),actionButton("pdfExtract","Extract",class = "btn-primary"),# actionButton("dataset",class = "btn-primary")
#Reading extracted datasets
# Input: Choose dataset ----
selectInput("dataset","Choose a dataset:",choices = c("SelectDataSet ","set1","set2","set3","set4")),# Button
downloadButton("downloadData","Download")
),mainPanel(
tableOutput("table"),# tableOutput("contents"),tabsetPanel(
tabPanel("PDF File select",h4("Domains"),tableOutput("table"),h3("Extracting..."),# selectInput("pdfExtract1",# tableOutput("preview"),# actionButton("pdfExtract",class = "btn-primary")
# downloadButton("download","Download .tsv")
),tabPanel("Raw data","TBD"),tabPanel("Summary data","TBD")
)
)
),# end of first tabpanel
tabPanel("calculation",sidebarPanel(
fileInput("file2",".csv")
),tags$hr(),checkBoxInput("header",textInput("txt2","domain info:","report"),sliderInput("slider",tags$h5("calculation"),actionButton("dataset2",class = "btn-primary")
),mainPanel(
tableOutput("contents2"),tabsetPanel(
tabPanel("Datasets",tableOutput("table2"),h3("Summarizing...")
)
)
)
),tabPanel("study compare",sidebarPanel(
fileInput("file3","compare:"),textInput("txt3",actionButton("action2","Compare",mainPanel(
tabsetPanel(
tabPanel("PDF File select",tableOutput("table3"),h3("Comparing..."),"TBD")
)
)
)
)
),server = function(input,output,session) {
# output$contents <- renderTable({
# # input$file1 will be NULL initially. After the user selects
# # and uploads a file,it will be a data frame with 'name',# # 'size','type',and 'datapath' columns. The 'datapath'
# # column will contain the local filenames where the data can
# # be found.
# inFile <- input$file1
#
# if (is.null(inFile))
# return(NULL)
#
# read.csv(inFile$datapath,header = input$header)
# })
# output$contents2 <- renderTable({
# # input$file1 will be NULL initially. After the user selects
# # and uploads a file,and 'datapath' columns. The 'datapath'
# # column will contain the local filenames where the data can
# # be found.
# inFile <- input$file2
#
# if (is.null(inFile))
# return(NULL)
# read.csv(inFile$datapath,header = input$header)
# })
# output$txtout <- renderText({
# paste(input$txt,input$slider,format(input$date),sep = ",")
# })
output$table <- renderTable({
df <- c("datasetsx")
})
output$table2 <- renderTable({
df <- c("datasetsy")
})
output$table3 <- renderTable({
df <- c("datasetsz")
})
observeEvent(input$dataset,{
source("domain.R",local = TRUE)
})
observeEvent(input$dataset2,{
source("calculation.R",local = TRUE)
})
#Domain level Selection
observeEvent(input$pdfExtract,{
if(input$pdfExtract1 == "PP"){
source("domain1.R",local = TRUE)
}
else if(input$pdfExtract1 == "MA"){
source("domain2.R",local = TRUE)
}
else if (input$pdfExtract1 =="CL"){
source("domain3.R",local =TRUE)
}
else{
source("domain4.R",local =TRUE)
}
})
# Reactive value for selected dataset ----
datasetInput <- reactive({
switch(input$dataset,"set1" = set2,"set2" = set2,"set3" = set3,"set4" = set4)
})
# Table of selected dataset ----
output$table <- renderTable({
datasetinput()
})
# Downloadable csv of selected dataset ----
output$downloadData <- downloadHandler(
filename = function() {
paste(input$dataset,".csv",sep = "")
},content = function(file) {
write.csv(datasetinput(),file,row.names = FALSE)
}
)
}
)
解决方法
我从你的代码中做了一个最小的例子。你必须做一些事情。 首先你需要一个 eventReactive:
datasetInput <- eventReactive( input$pdfExtract,{
switch(input$dataset,"set1" = set1
) })
如果用户选择了一个集合,什么都不会发生,直到用户点击“pdfExtract-button”。然后进行数据集输入。我们在这里调用它在我们的主面板中查看:
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
整个程序:
library(shiny)
set1 <- structure(list(This = c(1L,7L,3L),is_the = c(5L,8L,2L),header = c(9L,5L,4L)),class = "data.frame",row.names = c(NA,-3L))
options(shiny.maxRequestSize=30*1024^2)
ui <- fluidPage(
theme = "spacelab",selectInput(inputId = "dataset","Choose a dataset:",choices = c("SelectDataSet ","set1")),actionButton(inputId = "pdfExtract","Extract",class = "btn-primary"),mainPanel(
tableOutput(outputId = "table"),)
)
server = function(input,output,session) {
# Reactive value for selected dataset ----
datasetInput <- eventReactive( input$pdfExtract,{
switch(input$dataset,"set1" = set1
) })
# Table of selected dataset ----
output$table <- renderTable({
datasetInput()
})
}
shinyApp(ui,server)
另外你有 output$table
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。