R: Data Frame launching manually works well, in shiny is incomplete - r

I webscraped 2 tables (A and B). Then I merge them by rows (rbind). When I launch it, everything is ok. When I wanna use it in shiny app there is something wrong. Below structure of tables and what shiny shows. Any suggestion? Where could be the problem? Unfortunatelly, I can not show the code, because this is for my thesis. I would be very grateful for your help.
As you can see the problem is with third column. B table has all rows with NA. After merge, all data from A table has also NA.
In shiny table is showed by renderTable.
Structure of tables A and B

I have no answer for your question, but I would like to write something and there is not enough space for this in comment section, so I will write this as answer and eventually delete later. So - I rather believe that there is something wrong with your code which you use inside shiny and would like to check this with your help. I assume you need some help with debugging, so I will post a code below:
library(shiny)
ui <- fluidPage(
tableOutput("table")
)
server <- function(input, output, session) {
my_df <- reactive({
data.frame(a = c(1, 2, 3),
b = c(4, 5, 6))
})
output$table <- renderTable({
my_df()
browser()
})
}
shinyApp(ui, server)
In the code above I have made one output (table output) and - on the server side in reactive - I'm creating data.frame. Then I use this reactive function inside my output (output$plot). However, the last line inside output$plot is function browser() which is used for debugging. You can try my code in your console and see that when you run shiny app, it immediately moving back to console (but this is "dubugging state", so console looks a little different, for example there is a button "stop" with red square which can be use to close debugging state). Please run my shiny app and when you will be back in the console, in the debugging state, type my_df() to see the data.frame object. Could you please do the same with your shiny app? I mean, could you use browser() function on the last line in your renderTable? And come back and tell if the last column contains only NA or not when displayed in the console? If not, then I would say that you are doing something different in Shiny than manually with your tables.

Related

Shiny works locally but fails in production in weird manner

I have leaflet map in shiny dashboard with points. Clicking on some point, you will get text string from one column of main df botsad.final and subset of data from the SAME df, that should appear in the table below the map.
So:
1. Locally both text and table appear fine.
2. In production through shinyapps.io, text appears good, but table doesnt work. It produces error.
3. The df botsad.final is in directory of project and is same for textOutput (working) and tableOutput (working only locally).
Where is an error for deploying?
There is a part of server.R related to tableOutput. ui.R is here.
# Make a table with ecosystem services
output$table <- renderTable({
if (is.null(data_of_click$clickedMarker)) {
return(NULL)
}
return(
subset(botsad.final %>%
dplyr::select(7:12, 14),
id == data_of_click$clickedMarker$id
)
)
}, na = '-', bordered = T)
Are you receiving any error messages in the logs? I have not worked with leaflet maps in R, but I have deployed some applications to shinyapps.io. Please forgive my possibly unhelpful answer.
I had a look through the code, but could not run it (I think because I did not have the data).
My guess is that two things could be going wrong:
(1) the data is not being copied to the server. When you are deploying to R shiny, is the data in a file being deployed?
(2) the data is not being read on the server. Are you reading in the data in a global.R file or something?
If you are sure the data is on there, share what ever the logs are telling you.

Error: all observations are in the same group, while filtering the R model dynamically from shiny ui list

Guys i am new to Shiny and here i am facing issue with R to shiny translation,
i am using SelectInput to give teacher_IDs as input to filter the corresponding students data on sever.r.
Data looks like this
upto here below code works fine.
library(shiny)
x <- fluidPage("this is fluid page",
selectInput("Selecter","slide to select",choices = ID$Teacher_ID,
selected = "1003935242" ),
tableOutput("data")
################### server.r #################
)
shinyServer(function(input, output)
output$data = renderTable({
TeachersData[TeachersData$Teacher_ID==input$Selecter,]
})
})
But here below when i add more code that actually calculates statistical values using anova,kruskal etc in RenderTable() block like below, i get error message
Error:All observations are in the same group
***, i tried to resolve by doing split and creating groups with factor and few other things but cant resolve.
there is no problem in the code, it seems problem manipulation of the data with shiny way.
############ server.r problematic ##########
shinyServer(function(input, output) {
output$data = renderTable({
Instro = TeachersData[TeachersData$Teacher_ID==input$Selecter,]
Data_Procedure1= Instro
Data_Procedure1$Score=as.numeric(as.character(Data_Procedure1$Score))
Data_Procedure1_ByCourse=Subset_Data_ByCourse_fct(Data_Procedure1)
ANOVA_Procedure1= Compare_ANOVA_Procedure1_fct(Data_Procedure1,
Data_Procedure1$Score, as.factor(Data_Procedure1$Course_ID), alpha)
p_Procedure1 = ANOVA_Procedure1$p_value
Method_Procedure1= ANOVA_Procedure1$test_name
PairWise_Compare_Procedure1=Pairwise_Comparison_fct(Method_Procedure1,
Data_Procedure1, Data_Procedure1$Score,
as.factor(Data_Procedure1$Course_ID ))
result <- as.data.frame(PairWise_Compare_Procedure1)
})
})
Error message
Please anyone help to figure out, Thank you so much.
First of all, is this 'ID$Teacher_ID' loading ok? Are the options showing up at the UI? Sometimes this may be a problem.
Secondly, if I understood your problem correctly, you should change this:
TeachersData[TeachersData$Teacher_ID==input$slide,]
to this:
TeachersData[TeachersData$Teacher_ID==input$Selecter,]
If that is not the issue, maybe you should show what is input$slide?
Thank you #Ricardo for helping out, but the problem was something else.
after further digging out, i found that there is problem with the data set. because when i was filtering the data then some Teacher_ID's in data were having single records and no further sub-grouping, that's was statistical model was unable to capture observations of those Teacher_ID's and throwing error of having all observations in the same group. so when after i filtered out this type data that is not statistically significant everything works fine now.

Shiny App - passing the string to variable

I am trying to build Shiny App that does sentiment analysis. I have the code that works fine when i execute the script normally where Rstudio is importing the data from email.csv file. This file contains only 2 columns ( SentTo and RawText) and the text i am analyzing is located in B2 cell.
Once i run the code below i get nice chart that measure the sentiment.
library(readr)
library("ggplot2")
library('syuzhet')
Emails <- read_csv("C:/email.csv")
d<-get_nrc_sentiment(Emails$RawText)
td<-data.frame(t(d))
td_new <- data.frame(rowSums(td[1:14]))
names(td_new)[1] <- "count"
td_new <- cbind("sentiment" = rownames(td_new), td_new)
rownames(td_new) <- NULL
td_new2<-td_new[1:8,]
qplot(sentiment, data=td_new2, weight=count,
geom="bar",fill=sentiment)+ggtitle("Email sentiments")
Now what i am trying to do is to modify this code a bit and build the Shiny application by doing next:
ui.R
# Adding the Imput text field to the app
shinyUI(fluidPage(
textAreaInput("UserInput", "Caption", "Please Enter Your Text", width =
"500px", height = "300px"),
mainPanel(
plotOutput("distPlot"))
))
Server.R
library(shiny)
library(syuzhet)
library(ggplot2)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
output$distPlot <- renderPlot({
Emails <- input$UserInput
d<-get_nrc_sentiment(Emails)
td<-data.frame(t(d))
td_new <- data.frame(rowSums(td[1:14]))
names(td_new)[1] <- "count"
td_new <- cbind("sentiment" = rownames(td_new), td_new)
rownames(td_new) <- NULL
td_new2<-td_new[1:8,]
qplot(sentiment, data=td_new2, weight=count,
geom="bar",fill=sentiment)+ggtitle("Email sentiments")
})
})
After i run the app i get this error:
So ti builds the app but even when i paste some text in to the field it seems that the code i am using in server.R part is not doing what it needs to do.
If i replace the line in server.R part (Emails <- input$UserInput) with (Emails <- read_csv("C:/email.csv"))
than it works fine. This tells me that the issue is within the way i am passing the text to the Emails. Through the input form its text through the csv file it is a second row and second column that contains the data. The code that follows i think its looking that specific format.
Anybody has suggestion on how to modify this to make it work?
Thank you in advance.
I believe the issue is in line:
td_new <- data.frame(rowSums(td[1:14]))
If I change it to the following, it works for me:
td_new <- data.frame(rowSums(td))
I'm not sure why you had the 1:14 index in there, but I don't see what it does.
Before plotting in shiny, first have it output the user input so that you understand what is being passed to the next step. verbatimTextOutput('input$UserInput') and verbatimTextOutput('dput(input$UserInput)'). I'm guessing this will be a character vector of length 1.
Now, go back to your code outside of shiny and pass it this same input, formatted exactly the same. Right now your code that works is getting a data.frame from a csv file and passed a column, which would be a character vector.
Once you get it working outside of shiny, using the input as parsed by shiny, the fixes to make your shiny app work should be clear.

R shiny load data.frame for selectInput

I am trying to write a shiny app that loads several data frames. The data frames for my plots all work very well, but one data.frame which I want to use as a list of options in a dropdown menu does not load. If I load the frame seperately in the R session, everything works, but if I only run the shiny app, the selectors won't update.
library(shiny)
ui <- fluidPage(
#...
selectInput("mats", "Text",
selectors)
# ...
)
server <- function(input, output){
# ...
df1=read.csv("./data/file.csv", sep=";", head=T, stringsAsFactors = F)
df1$choices=as.character(df1$choices)
selectors=c("All", df1$choices)
#...
}
shinyApp(ui = ui, server = server)
I think, I need the selectors in the server function, so I loaded the data frame there together with my other data frames. Is that the right place and what do I need to do to get this running?
Best
There is a number of problems with your code:
Following the documentation, choices in selectInput should be a list.
If you want to create an element that would be available across the ui and server please consider reading the linked article on scoping rules in Shiny and defining your object in global.R.
With respect to the 1st point, if your intention is to use a data.frame column as a base for menu selection, you can apply the following transformation:
my_new_list <- split(df$id, df$subject)
as provided in this answer by #user3710546 to a similar question that I've asked in past.
Side note
Please consider having a look at the discussion on making a reproducible example in R. If you care to redo your example using some publicly available data, it will be easy to produce a solution. If I understood the problem correctly, you want to use data.frame column as a base for UI element, which is not difficult on its own.
.

Isolate no longer works since addition of eventReactive and observeEvent?

Previously, I had built a shiny dashboard with chart outputs that worked just fine and looked like the following:
output$someName <- renderGvis({
input$inputButton
data <- isolate(myData(function here))
donut <- gvisDonut({...})
return(donut)
})
Since the addition of observeEvent and eventReactive, I've not been able to get it to work the same as before. Essentially, the output$someName is a chart that is dependent on multiple inputs, and each time the user clicks on the inputButton, I need renderGvis to re-evaluate. The function should NOT re-evaluate when any of the other inputs change, just when the button is pressed.
I've had some luck getting observeEvent to run on input$inputButton click, however, each time I change any of my input parameters, the query is quickly rerun without having to press the button. Any takers here?
More detailed below:
output$someName <- renderGvis({
input$inputButton
data <- isolate(dataGrabber({})) # function that takes input and returns data frame using RMySQL
isolate(simpleChart(data = data)) # simpleChart is a function to produce a gvisCalendar chart.
OK...found an answer to this if anyone ever has this problem. The issue, which for some reason I had not encountered in the past, is that the isolate function now runs regardless of whether or not the value for actionButton is 0 or not. I believe in the past, it wouldn't run until actionButton had a value greater than 0.
The simple fix was:
output$someName <- renderGvis({
input$inputButton
if (input$inputButton == 0)
return()
isolate({ code to isolate})
})

Resources