0
我有困难把一些代码放到一个单独的文件中。在哪里把代码工作在输入文件(Shiny,R)
下面的代码工作:
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput('file1', 'Choose CSV File',
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
tags$hr()
),
mainPanel(
tableOutput('contents')
)
)
))
server.R
library(shiny)
library(dplyr)
shinyServer(function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data = read.csv(inFile$datapath)
### CleanData
data = data %>%
mutate_each(funs(toupper)) %>%
mutate_each(funs(gsub("[[:punct:]]", " ", .))) %>%
mutate_each(funs(str_trim)) %>%
mutate_each(funs(rm_white)) %>%
sample_frac(1)
})
output$contents = renderTable({
getData()
})
})
但是如果我把一些代码放到一个单独的文件,这不是”工作。
ui.R不会改变
server.R
library(shiny)
library(dplyr)
shinyServer(function(input, output) {
getData <- reactive({
inFile <- input$file1
if (is.null(inFile)) return(NULL)
data = read.csv(inFile$datapath)
### CleanData
source('CleanData.R')
})
output$contents = renderTable({
getData()
})
})
CleanData.R(在笔记本电脑上的同一目录)
data = data %>%
mutate_each(funs(toupper)) %>%
mutate_each(funs(gsub("[[:punct:]]", " ", .))) %>%
mutate_each(funs(str_trim)) %>%
mutate_each(funs(rm_white)) %>%
sample_frac(1)
它不工作,给错误
Error in UseMethod("tbl_vars") :
no applicable method for 'tbl_vars' applied to an object of class "function"
有人知道如何解决这个问题?非常感谢
嗨,复合问题通常气馁。将你的问题分解成单个问题可能是有意义的。 – pete23
谢谢@ pete23我会编辑它。 –
'CleanData.R'中是否有函数?尝试在'reactiveServer(函数(输入,输出))'行后面调用'source('CleanData.R')''。然后,在你的'getData()'中调用CleanData中的函数名称 – tospig