As an absolute beginner, I'd appreciate some help as to why the last "print" ( the print("Good bye!") statement) comes up as "syntax error" I'm using Python 3.4 but am working through a 2... tutorial.
fruits = ['banana', 'apple', 'mango'] #Create list
for fruit in fruits:
print ('Current fruit :', fruit)
print ("Good bye!")
If I simply enter a blank line and do not indent it works, is this my error or a change in the syntax for 3.4?
Related
Quick backstory:
I've already done quite a few other R scripts in VSCode. I have the R extension, I've knitted .rmd's, etc. etc...
So in other words, unlike the solutions posted here which dealt more with getting R to work in the first place (in visual studio at least), I've already got R working for most things within VSCode.
So I have an R file that I'll put the code to below, but when I open the file in RStudio, it works great! It creates a jdbc connection, queries a database using some SQL, creates a dataframe, etc...
When I close that file, and then open it in VSCode, it'll run MOST of the r code within it, but when it goes to do the query, I get this sql error:
"JDBC ERROR: ORA-00907: missing right parenthesis"
And its the exact same file! I'd google what ORA-00907 means for SQL and how to fix it.... but the code DOES work in Rstudio?
One other thing I noticed is the problem does NOT happen when I run the file as a whole from within VsCode, i.e.:
source("BlackBox.R")
If I do that, I'll step through everything and save out the results of the query as a .csv like I want it to. But if I OPEN the file and go through line by line, or try to run the whole thing, or anything.... it won't work.
Code below (with names changed to protect the innocent):
library(tidyverse)
Sys.setenv(JAVA_HOME="C:\\Program Files\\TIBCO\\Jaspersoft Studio-6.6.0\\features\\jre.win32.win32.x86_64.feature_1.8.0.u171\\jre")
options(java.parameters="-Xmx2g")
replacement <- function(category = "LC_ALL") {
if (identical(category, "LC_MESSAGES"))
return("")
category <- match(category, .LC.categories)
if (is.na(category))
stop("invalid 'category' argument")
.Internal(Sys.getlocale(category))
}
base <- asNamespace("base")
environment(replacement) <- base
unlockBinding("Sys.getlocale", base)
assign("Sys.getlocale", replacement, envir = base)
lockBinding("Sys.getlocale", base)
library(rJava)
rJava::.jinit()
library(RJDBC)
jdbcDriver <- JDBC(driverClass="oracle.jdbc.driver.OracleDriver", classPath="C:/Users/johnDoe/OneDrive - Company/Documents/Data/jar files/ojdbc8.jar")
jdbcConnection <- dbConnect(jdbcDriver, "jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS=(PROTOCOL=tcps)(HOST=website.com)(PORT=2879))(CONNECT_DATA=(SERVICE_NAME=pdblhs)))", "name", "password")
Subjects <- dbGetQuery(jdbcConnection,
"WITH usa AS (
SELECT subj_access.protocol_id
, subj_access.protocol_subject_id
FROM website.sv_user_pcl_permission priv_check
JOIN website.sv_user_pcs_access subj_access ON priv_check.protocol_id = subj_access.protocol_id AND priv_check.contact_id = subj_access.contact_id
WHERE priv_check.function_name = 'CRPT-Subject Visits'
AND priv_check.contact_id = '1234')
, subjects AS (
SELECT Protocol_id, Protocol_no, Protocol_subject_id,subject_no, sequence_number, status,status_date
FROM website.RV_SUBJECT_STATUS
)
, all_visit AS (
SELECT protocol_subject_id, visit_name, visit_date, planned_visit_date, visit_desc FROM website.rv_sub_calendar WHERE visit_status = 'Planned' and visit_date > ADD_MONTHS(SYSDATE, 0)
)
, max_visit AS (
SELECT protocol_subject_id, visit_name, visit_date, planned_visit_date, visit_desc FROM LHS_ONCORE_PROD.all_visit WHERE (visit_date, protocol_subject_id) IN (SELECT MIN(visit_date) visit_date, protocol_subject_id FROM all_visit GROUP BY protocol_subject_id)--
)
, followup AS (
SELECT protocol_subject_id, off_studydate, off_study_reason FROM website.rv_subject_follow_up
)
SELECT subjects.protocol_id
, subjects.protocol_no
, subjects.protocol_subject_id
, subjects.subject_no
, subjects.sequence_number
, subjects.status
, subjects.status_date
, max_visit.visit_date
, max_visit.planned_visit_date
, max_visit.visit_name
, max_visit.visit_desc
, followup.off_studydate
, followup.off_study_reason
FROM usa
INNER JOIN subjects ON subjects.Protocol_subject_id = usa.protocol_subject_id
LEFT JOIN max_visit ON max_visit.protocol_subject_id = usa.protocol_subject_id
LEFT JOIN followup ON followup.Protocol_subject_id = subjects.Protocol_subject_id
")
# Close connection
dbDisconnect(jdbcConnection) ##Closes it.
setwd("A:/Project Documents/R Database connections/place")
write_csv(Subjects,"Subject.csv")
Any ideas? At least where to look?
Update:
It's something to do with the spacing in VSCode. For instance if I run this simple command on one line, it'll recognize it as a new object and save it into the environment:
But if I run this command thats split over two lines, it'll trap me in an endless loop of looking for a right parenthesis or something:
And when I highlight several lines from the query, it'll show me different spacing/indentations in the beginning of each line even though they all look the same:
I'm currently extracting phenotype/covariate data I need to run a GWAS. I am also trying to convert the 'bank_centre location' to a dummy variable. In attempting to create the data file to run the GWAS, I keep getting an error for an extra "]" in my code relating to the dummy variable, however I don't see how there is an extra "]" in my code.
this is the first part of my input:
source scriptSettings.sh
Rscript - <<'END'
library(data.table)
d<-fread("phenos.txt")
message("create array dummy")
d[,array01:=as.numeric(as.numeric(f.22000.0.0)>1)]
message ("create bank_centre dummy")
d[,'bank1']<-0
d[,'bank2']<-0
d[,'bank3']<-0
d[which(d[,'f.54.2.0']==11025,'bank1']<-1
d[which(d[,'f.54.2.0']==11026,'bank2']<-1
d[which(d[,'f.54.2.0']==11027,'bank3']<-1
The output I get so far is:
create array dummy
create biobank_centre dummy
Error: unexpected ']' in "d[which(d[,'f.54.2.0']==11025,'bank1']"
Execution halted
Apologies if I am missing something super obvious - any help is much appreciated
I think there is a missing ) for which.
i.e. You may want to try something like this: d[which(d[,'f.54.2.0']==11025),'bank1']<-1
I am learning from the book Learn Python The Hard Way 3.6, by Zed Shaw
There are a series of 6 target.write commands towards the bottom of the script and he wants me to simplify them into a single target.write command using strings formats and escapes. However, I am stuck.
Here is the original code:
from sys import argv
script, filename = argv
print(f"We're going to erase {filename}")
print("If you don't want that, hit CTRL-C (^C).")
print("If you do want that, hit RETURN.")
input("?")
print("Opening the file...")
target = open(filename,'w')
print("Truncating the file. Goodbye!")
target.truncate()
print("Now I'm going to ask you for three lines")
line1 = input("line 1:")
line2 = input("line 2:")
line3 = input("line 3:")
print("Im going to write these to the file.")
target.write(line1)
target.write("\n")
target.write(line2)
target.write("\n")
target.write(line3)
target.write("\n")
print("And finnaly, we close it")
target.close()
So far I have tried
target.write(line1),(line2),(line3)
but this gives a logical error of only writing to one line not all three.
target.write(line1) + (line2) + (line3)
with this one I get error
'unsupported operand types for +: 'int' + 'str'
target.write(line1),\n,(line2)\n(line3),\n
with this one I get error:
unexpected character after line continuation character
(<string>,line 22)
I have been googling and searching here for answers but have not found anything. One person posted a very similar question except for Zed's 2.7 book. However I am reading Zed's 3.6 book so the answers were no help to me unfortunately.
I'm not sure what you have and haven't covered so far in the book as I'm not familiar with it but one way to do what you want is to format the string first and then pass it to the write method like this:
target.write("{0}\n{1}\n{2}\n".format(line1, line2, line3))
I'm doing text parsing in Julia and need to test if certain strings are blank (in order to parse into floats). I have been using isblank() booleans in <0.4 Julia, but after upgrading to 0.4, I get the following warning:
julia> isblank(q)
WARNING: isblank(s::AbstractString) is deprecated, use all((c->begin
c == ' ' || c == '\t'
end),s) instead.
in depwarn at deprecated.jl:73
in isblank at deprecated.jl:50
while loading no file, in expression starting on line 0
true
What replaced isblank()? Is the above really the replacement? My googling didn't turn up anything useful.
Yes, that is the replacement.
Basically in #5939 and related issues, it was revealed that answering isblank is a lot harder than it might first appear given Unicode complexities.
The deprecation occurred in #8233.
Everything looks natural, it must be a replacement in new version, you can add the line bellow in your code and replace (using Ctrl+H) all isblank( with isblk(. I wish it works for you.
isblk(s)=all((c->begin c == ' ' || c == '\t' end),s)
now isblk([]) # => true
I am using lintr to ensure my code meets good R style guidelines, but for some reason, the commas_linter does not catch it when I do not put a space after a comma if there is a blank line preceding the erroneous line. Am I doing something wrong, or is this a bug in the package?
Here's a MWE
file: Test.r
#random comment
a <- c(4,5)
b = 7
Then, I run the following:
lintr::lint("./Test.r", linters = list(spacesAfterCommas = commas_linter,
useArrowForAssignments = assignment_linter))
The only error I get is the following:
./Test.r:4:3: style: Use <-, not =, for assignment.
b = 7
^
Why is the commas_linter not catching my error?