【发布时间】:2020-04-18 22:47:30
【问题描述】:
我想上传 csv/txt 文件,其中包含 id、时间、浓度和绘图(时间(x 轴)与浓度(y 轴)。我的应用程序启动并崩溃。当我试图将 updateelctinput 函数带到外面时reactive闪亮的应用程序没有运行。
有人能告诉我为什么会这样吗?
我在下面粘贴了我的代码:
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title="Dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem("Input Data", tabName = "dashboard", icon = icon("dashboard")))),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
tabPanel("Input Route",
# First tab content
fileInput("df", "Choose text/csv File",
multiple = FALSE,
accept = c("text/csv"))),
tags$br(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
tabPanel("First Type",
# "Empty inputs" - they will be updated after the data is uploaded
selectInput('xcol', 'X Variable', ""),
selectInput('ycol', 'Y Variable', "", selected = ""),
selectInput('ide','Group',"",selected="")
))),
fluidRow(
box(plotOutput("Plot1"),height = 250)
)
))
server <- function(input, output,session) {
data <- reactive({
inFile <- input$df
df <- read.csv(inFile$datapath, header = input$header)
return(df)
})
observe({
df1 = data()
updateSelectInput(session,inputId = 'xcol', label = 'X Variable',
choices = names(df1), selected =names(df1)[2])
updateSelectInput(session,inputId = 'ycol', label = 'Y Variable',
choices = names(df1), selected = names(df1)[2])})
### Plot
output$Plot1<-renderPlot({plot(Plot1<-data()%>%ggplot()+
geom_line(aes(x=input$xcol,y=input$ycol))+
theme_bw())})
}
shinyApp(ui, server)
【问题讨论】:
-
input$df是应用程序启动时的NULL。在data电抗导体的开头添加req(input$df)。这将阻止所有内容,直到您上传文件。aes(x=input$xcol,y=input$ycol)也不起作用,因为input$xcol和input$ycol是字符串;使用aes_string而不是aes。 -
嗨!我根据建议更新了代码仍然不起作用。
-
Warning: Error in UseMethod: no applicable method for 'req' applied to an object of class "NULL"这是我在编译时遇到的错误。闪亮和服务器 -
@Ravua1992,这很奇怪。您正在加载另一个包裹吗?
shiny::req(input$df)有效吗? -
@StéphaneLaurent 是的,也尝试过,但没有帮助。粘贴下面的代码供您查看。
标签: r shiny shinydashboard